package com.zhiyou.app.shop.website.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou.app.shop.common.util.Page;
import com.zhiyou.app.shop.website.dao.IMoneyDealDao;
import com.zhiyou.app.shop.website.dao.IUserAccountDao;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.dao.pojo.UserStockInfo;
import com.zhiyou.app.shop.website.service.IUserAccountService;

@Service(value="userAccountService")
public class UserAccountServiceImpl implements IUserAccountService {
	
	@Autowired
	private IUserAccountDao userAccountDao;
	@Autowired
	private IMoneyDealDao moneyDealDao;

	public UserAccount findUserAccount(String userId) {
		List<UserAccount> list = userAccountDao.findByProperty( "userId", userId);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public UserAccount findByUserAccount(String userAccount) {
		List<UserAccount> list = userAccountDao.findByProperty( "userId", userAccount);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	public void findAll(Page page, User user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserAccount.class);
	}

	public void doCharge(UserAccount userAccount, MoneyDeal deal) {
		userAccountDao.update(userAccount);
		moneyDealDao.save(deal);
	}

	public void splitStock(float per) {
		userAccountDao.splitStock(per);
	}

	public List<UserStockInfo> listSaleOrBuy(String userid,String tradeType,float startprice,
			float endprice,String card,String name,int pageno,int pagesize){
		String hql = "from UserStockInfo usi where usi.status = '0' ";
		Map<String,Object> param = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(userid)){
			hql = hql + " and usi.userId = :userId ";
			param.put("userId", userid);
		}
		if(StringUtils.isNotBlank(tradeType)){
			hql = hql + " and usi.type = :type ";
			param.put("type", tradeType);
		}
		if(StringUtils.isNotBlank(card)){
			hql = hql + " and usi.userId in (select ub.userId from UserBank ub where ub.bankCard = :bankCard )";
			param.put("bankCard", card);
		}
		if(StringUtils.isNotBlank(name)){
			hql = hql + " and usi.userId in (select ub.userId from UserBank ub where ub.bankCard = :bankUser )";
			param.put("bankUser", name);
		}
		if(startprice>0){
			hql = hql + " and usi.price >= :startprice ";
			param.put("startprice", startprice);
		}
		if(endprice>0){
			hql = hql + " and usi.price <= :endprice ";
			param.put("endprice", endprice);
		}
		
		return userAccountDao.listSaleOrBuy(hql, param,pageno,pagesize);
	}
	
	public List listNum(String userid,String level,String tradeType,float startprice,float endprice,
			String status,Date starttime,Date endtime,String card,String name,int startnum,int endnum,int pageno,int pagesize){
		String hql = "from UserAccount ua where 1 = 1 ";
		Map<String,Object> param = new HashMap<String,Object>();
		if(StringUtils.isNotBlank(userid)){
			hql = hql + " and ua.userId = :userId ";
			param.put("userId", userid);
		}
		if(StringUtils.isNotBlank(level)&&!"0".equals(level)){
			hql = hql + " and ua.userId in (select u.recommendId from User u where u.level = :level ) ";
			param.put("level", level);
		}
		if(StringUtils.isNotBlank(card)){
			hql = hql + " and ua.userId in (select ub.userId from UserBank ub where ub.bankCard = :bankCard )";
			param.put("bankCard", card);
		}
		if(StringUtils.isNotBlank(name)){
			hql = hql + " and ua.userId in (select ub.userId from UserBank ub where ub.bankCard = :bankUser )";
			param.put("bankUser", name);
		}
		if(startnum>0){
			hql = hql + " and ua.stockNum >= :startnum ";
			param.put("startnum", startnum);
		}
		if(endnum>0){
			hql = hql + " and ua.stockNum <= :endnum ";
			param.put("endnum", endnum);
		}
		hql = hql + " order by ua.stockNum desc ";
		List list = userAccountDao.listSaleOrBuy(hql, param,pageno,pagesize);
		return list; 
	}

	public void saveAccount(UserAccount account) {
		userAccountDao.save(account);
	}

	public BigDecimal getTotal(String account) {
		return userAccountDao.getTotalAccount(account);
	}

}
